package com.ccsoft.multds.aspect;

import com.ccsoft.multds.annotion.WR;
import com.ccsoft.multds.config.DynamicDataSource;
import com.ccsoft.multds.utils.MultDSSwitcher;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

/**
 * @author chanchaw
 * @create 2022-09-17 8:18
 */
@Component
@Aspect
public class DynamicDataSourceAspect {

    // 检测指定包下的含有自定义注解@WR的方法
    // 执行前置通知
    @Before("within(com.ccsoft.multds.service.*) && @annotation(wr)")
    public void before(JoinPoint joinpoint, WR wr){
        MultDSSwitcher dsFlag = wr.value();
        DynamicDataSource.multDSSwitcher.set(dsFlag);
        System.out.println("已设置即将切换的数据源flag:" + dsFlag.getDesc());
    }
}
